﻿@page "/Albums"
@using Jx.Toolbox.Extensions
@using JxAudio.TransVo
@using JxAudio.Front.Data
@using JxAudio.Front.Filter
@using Microsoft.Extensions.Localization

@inject HttpClient Http
@inject IDispatchService<AddTrackMessage> DispatchService
@inject IStringLocalizer<Albums> StringLocalizer

<PageTitle>@StringLocalizer["Title"] - JxAudio</PageTitle>

<div style="height: calc(100vh - 5.1rem - 82px)">
    <FrontTable TItem="AlbumVo" ShowToolbar="true" ShowDefaultButtons="false" ShowExtendButtons="true"
                ShowExtendDeleteButton="false" ShowExtendEditButton="false" OnQueryAsync="OnQueryAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.CoverId" Width="40">
                <Template Context="value">
                    <Avatar Url="@($"/api/Cover?coverId={value.Value}")"></Avatar>
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Title" Searchable="true" Sortable="true" Filterable="true">
                <Template Context="value">
                    <LinkButton Text="@value.Value" Url=@($"/Album/{value.Row.Id}")></LinkButton>
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Artist" Searchable="true" Sortable="true" Filterable="true" CustomSearch="SearchArtist">
                <FilterTemplate>
                    <ArtistFilter></ArtistFilter>
                </FilterTemplate>
                <Template Context="value">
                    @if (value.Value != null)
                    {
                        <LinkButton Url="@($"/Artist/{value.Value.Id}")">@value.Value?.Name</LinkButton>
                    }
                    else
                    {
                        @:未知歌手
                    }
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Count"></TableColumn>
            <TableColumn @bind-Field="@context.TotalSize">
                <Template Context="value">
                    @value.Value.ToSizeString()
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.TotalTime">
                <Template Context="value">
                    @value.Value.ToTimeString()
                </Template>
            </TableColumn>
        </TableColumns>
        <RowButtonTemplate>
            <TableCellButton Icon="fa fa-play" OnClick="@(() => PlayAll(context, PlayType.Replace))"></TableCellButton>
            <TableCellButton Icon="fa fa-plus" OnClick="@(() => PlayAll(context, PlayType.Add))"></TableCellButton>
            <TableCellButton Icon="@(context.Star? "fas fa-heart" : "far fa-heart")"
                             OnClick="() => Star(context)" style="color: red"></TableCellButton>
        </RowButtonTemplate>
    </FrontTable>
</div>

@code {
    

    private async Task<QueryData<AlbumVo>> OnQueryAsync(QueryPageOptions queryPageOptions)
    {
        QueryOptionsVo queryOptionsVo = new QueryOptionsVo
        {
            SortName = queryPageOptions.SortName.ToFieldName(),
            DynamicFilterInfo = queryPageOptions.ToDynamicFilter(),
            SortOrder = (int)queryPageOptions.SortOrder,
            IsPage = queryPageOptions.IsPage,
            PageIndex = queryPageOptions.PageIndex,
            PageItems = queryPageOptions.PageItems
        };
        var responseMessage = await Http.PostAsJsonAsync("/api/Album", queryOptionsVo);
        var vo = await responseMessage.Content.ReadFromJsonAsync<QueryData<AlbumVo>>();
        if (vo == null)
        {
            return new QueryData<AlbumVo>();
        }

        return vo;
    }

    private async Task PlayAll(AlbumVo albumVo, PlayType type)
    {
        var responseMessage = await Http.GetAsync($"/api/Album/AllTracks?id={albumVo.Id}");
        var tracks = await responseMessage.Content.ReadFromJsonAsync<List<TrackVo>>();
        if (tracks != null)
        {
            DispatchService.Dispatch(new DispatchEntry<AddTrackMessage>()
            {
                Entry = new AddTrackMessage()
                {
                    Tracks = tracks,
                    Type = type
                }
            });
        }
    }

    private SearchFilterAction SearchArtist(ITableColumn tableColumn, string? value)
    {
        return new SearchFilterAction(tableColumn.GetFieldName(), value);
    }

    private async Task Star(AlbumVo albumVo)
    {
        if (albumVo.Star)
        {
            if (await Http.GetStringAsync("/api/Album/UnStar?id=" + albumVo.Id) == "s")
            {
                albumVo.Star = false;
            
            }
        }
        else
        {
            if (await Http.GetStringAsync("/api/Album/Star?id=" + albumVo.Id) == "s")
            {
                albumVo.Star = true;
            }
        }
        
    }

}